import { combineReducers } from 'redux'
import { CENTER_LIST, CHANGE_IDENTITY, REMOVE_CHAT, SAVE_BOX, SAVE_CHATTING, SAVE_MSG_AVATAR, SAVE_NAME, TOKEN } from './action-type'

const initState = {
    title: ''
}

const tokenState = {
    token: ''
}

const centerState = {
    list: ''
}

const MsgState = {
    avatar: [],
    chatObj: [],
    box: '',
    name: ''
}

function MsgMode(state = MsgState, action) {
    if (action.type == SAVE_MSG_AVATAR) {
        let newAvatar = state.avatar
        newAvatar.push(action.avatar)
        return { ...state, avatar: newAvatar }
    } else if (action.type == SAVE_CHATTING) {
        let newObj = state.chatObj
        newObj.push(action.chatObj)
        return { ...state, chatObj: newObj }
    } else if (action.type == SAVE_BOX) {
        return { ...state, box: action.box }
    } else if(action.type == SAVE_NAME) {
        return {...state, name: action.name}
    } else if(action.type == REMOVE_CHAT) {
        return {...state,chatObj: action.chatObj}
    }
    return state
}

function IdentityMode(state = initState, action) {
    if (action.type == CHANGE_IDENTITY) {
        return { ...state, title: action.title }
    }
    return state
}

function TokenMode(state = tokenState, action) {
    if (action.type == TOKEN) {
        return { ...state, token: action.token }
    }
    return state
}

function CenterMode(state = centerState, action) {
    if (action.type == CENTER_LIST) {
        return { ...state, centerList: action.centerList }
    }
    return state
}

export default combineReducers({
    IdentityMode,
    TokenMode,
    CenterMode,
    MsgMode
})